package com.zhihao.zhihaopicturebackend.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Jackson
 * @create 2025/1/7 20:20
 * @description url图片文件名提取工具类
 */
public class FileNameExtractor {
    public static String extractFileName(String fileUrl) {
        // 正则表达式匹配最后一个斜杠后的文件名，文件名后缀为常见的图片格式
        String pattern = ".*/([^/?=]+\\.(jpg|png|webp|jpeg|gif|bmp))";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(fileUrl);
        if (m.find()) {
            return m.group(1);
        } else {
            return fileUrl;
        }
    }

    public static void main(String[] args) {
        String fileUrl1 = "https://vigen-invi.oss-cn-shanghai.aliyuncs.com/service_dashscope/ImageOutPainting/2025-01-07/public/bf3d0203-8cd5-4522-b856-dedd0798f3fd/result-3075e5f4-549a-490c-b383-a028d23f3cb0.jpg?OSSAccessKeyId=LTAI5t7aiMEUzu1F2xPMCdFj&Expires=1736251534&Signature=8Qc0E6IsNxDWfgH2uAPYVlMJGGI=";
        String fileUrl2 = "https://vigen-invi.oss-cn-shanghai.aliyuncs.com/service_dashscope/ImageOutPainting/2025-01-07/public/bf3d0203-8cd5-4522-b856-dedd0798f3fd/result-3075e5f4-549a-490c-b383-a028d23f3cb0.jpg===";
        String fileUrl3 = "https://example.com/path/to/image/webp_image.webp";
        String fileName1 = extractFileName(fileUrl1);
        String fileName2 = extractFileName(fileUrl2);
        String fileName3 = extractFileName(fileUrl3);
        System.out.println("Extracted file name 1: " + fileName1);
        System.out.println("Extracted file name 2: " + fileName2);
        System.out.println("Extracted file name 3: " + fileName3);
    }
}

